<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Nginx + Tomcat 配置负载均衡集群 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Nginx + Tomcat 配置负载均衡集群</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Nginx/">Nginx</a> <a class="article__tag-link" href="/tags/Tomcat/">Tomcat</a>
			</span>
		
	</div>

	

	
		<h2 id="一、Hello-world"><a href="#一、Hello-world" class="headerlink" title="一、Hello world"></a>一、Hello world</h2><h3 id="前期环境准备"><a href="#前期环境准备" class="headerlink" title="前期环境准备"></a>前期环境准备</h3><ol>
<li><p>准备两个解压版tomcat，如何同时启动两个tomcat，请看我的另一篇文章《一台机器同时启动多个tomcat》。</p>
</li>
<li><p>nginx官网下载解压版nginx。</p>
</li>
<li><p>创建一个简单的web项目。为了直观的区分访问的哪个tomcat，在页面写上标记8081、8082。</p>
</li>
</ol>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nginx%20+%20Tomcat%20%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4/01.png?raw=true" alt=""></figure></p>
<ol>
<li>分别部署到对应的tomcat下。如图：</li>
</ol>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nginx%20+%20Tomcat%20%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4/02.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nginx%20+%20Tomcat%20%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4/03.png?raw=true" alt=""></figure></p>
<h3 id="配置nginx"><a href="#配置nginx" class="headerlink" title="配置nginx"></a>配置nginx</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;进入nginx-1.10.1conf路径，修改配置文件nginx.conf。</p>
<ol>
<li>配置服务器组，在http{}节点之间添加upstream配置。（注意不要写localhost，不然访问速度会很慢）</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">upstream nginxDemo &#123;</div><div class="line">server 127.0.0.1:8081;   <span class="comment">#服务器地址1</span></div><div class="line">server 127.0.0.1:8082;   <span class="comment">#服务器地址2</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ol>
<li>修改nginx监听的端口号80，改为8080。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">server &#123;</div><div class="line">    listen       8080;</div><div class="line">    ......</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ol>
<li>在location{}中，利用proxy_pass配置反向代理地址；此处“<a href="http://”不能少，后面的地址要和第一步upstream定义的名称保持一致。" target="_blank" rel="external">http://”不能少，后面的地址要和第一步upstream定义的名称保持一致。</a></li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">location / &#123;</div><div class="line">root   html;</div><div class="line">index  index.html index.htm;</div><div class="line">proxy_pass http://nginxDemo; <span class="comment">#配置方向代理地址</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如下图：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nginx%20+%20Tomcat%20%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4/04.png?raw=true" alt=""></figure></p>
<h3 id="启动nginx和tomcat，访问"><a href="#启动nginx和tomcat，访问" class="headerlink" title="启动nginx和tomcat，访问"></a>启动nginx和tomcat，访问</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我是Windows系统，所以直接在nginx-1.10.1目录下双击nginx.exe即可。<br>可在任务管理器中查看</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nginx%20+%20Tomcat%20%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4/05.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后在浏览器输入地址：<a href="http://localhost:8080/nginxDemo/index.jsp，每次访问就会轮流访问tomcat了（如果F5刷新不管用，建议试试鼠标指针放到地址栏，点击Enter键）。" target="_blank" rel="external">http://localhost:8080/nginxDemo/index.jsp，每次访问就会轮流访问tomcat了（如果F5刷新不管用，建议试试鼠标指针放到地址栏，点击Enter键）。</a></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nginx%20+%20Tomcat%20%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4/06.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nginx%20+%20Tomcat%20%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4/07.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;到这里，一个非常简单的负载均衡就配置完成了，是不是很简单呢，O(∩_∩)O哈哈~</p>
<h2 id="二、nginx负载均衡策略"><a href="#二、nginx负载均衡策略" class="headerlink" title="二、nginx负载均衡策略"></a>二、nginx负载均衡策略</h2><h3 id="轮询（默认）"><a href="#轮询（默认）" class="headerlink" title="轮询（默认）"></a>轮询（默认）</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每个web请求按时间顺序逐一分配到不同的后端服务器，如果后端服务器down掉，能自动剔除。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">upstream nginxDemo &#123;</div><div class="line">      server 127.0.0.1:8081;</div><div class="line">      server 127.0.0.1:8082;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="最少链接"><a href="#最少链接" class="headerlink" title="最少链接"></a>最少链接</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;web请求会被转发到连接数最少的服务器上。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">upstream nginxDemo &#123;</div><div class="line">      least_conn;</div><div class="line">      server 127.0.0.1:8081;</div><div class="line">      server 127.0.0.1:8082;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="weight-权重"><a href="#weight-权重" class="headerlink" title="weight 权重"></a>weight 权重</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定轮询几率，weight和访问比率成正比，用于后端服务器性能不均的情况，weight默认是1。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;服务器A和服务器B的访问比例为：2-1;比如有3个请求，前两个会访问A，三个访问B，其它规则和轮询一样。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">upstream nginxDemo &#123;</div><div class="line">      server 127.0.0.1:8081 weight=2; <span class="comment">#服务器A</span></div><div class="line">      server 127.0.0.1:8082; <span class="comment">#服务器B</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="ip-hash"><a href="#ip-hash" class="headerlink" title="ip_hash"></a>ip_hash</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每个请求按访问ip的hash值分配，这样同一客户端连续的Web请求都会被分发到同一服务器进行处理，可以解决session的问题。当后台服务器宕机时，会自动跳转到其它服务器。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">upstream nginxDemo &#123;</div><div class="line">      ip_hash;</div><div class="line">      server 127.0.0.1:8081 weight=2; <span class="comment">#服务器A</span></div><div class="line">      server 127.0.0.1:8082; <span class="comment">#服务器B</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;基于weight的负载均衡和基于ip_hash的负载均衡可以组合在一起使用。</p>
<h3 id="url-hash（第三方）"><a href="#url-hash（第三方）" class="headerlink" title="url_hash（第三方）"></a>url_hash（第三方）</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;url_hash是nginx的第三方模块，nginx本身不支持，需要打补丁。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nginx按访问url的hash结果来分配请求，使每个url定向到同一个后端服务器，后端服务器为缓存服务器、文件服务器、静态服务器时比较有效。缺点是当后端服务器宕机的时候，url_hash不会自动跳转的其他缓存服务器，而是返回给用户一个503错误。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">upstream nginxDemo &#123;</div><div class="line">      server 127.0.0.1:8081; <span class="comment">#服务器A</span></div><div class="line">      server 127.0.0.1:8082; <span class="comment">#服务器B</span></div><div class="line">      <span class="built_in">hash</span> <span class="variable">$request_url</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="fair（第三方）"><a href="#fair（第三方）" class="headerlink" title="fair（第三方）"></a>fair（第三方）</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;按后端服务器的响应时间来分配请求，响应时间短的优先分配。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">upstream nginxDemo &#123;</div><div class="line">      server 127.0.0.1:8081; <span class="comment">#服务器A</span></div><div class="line">      server 127.0.0.1:8082; <span class="comment">#服务器B</span></div><div class="line">      fair;</div><div class="line">&#125;</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/Tomcat/7. Nginx + Tomcat 配置负载均衡集群/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
